package com.hs.sfzy.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Article;
import com.hs.sfzy.entity.Publicity;
import com.hs.sfzy.service.PublicityService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PublicityController
 * @Description TODO
 * @Author msj
 * @Date 2024年10月23日 18:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/publicity")
public class PublicityController {

    @Autowired
    private PublicityService publicityService;

    @PostMapping
    public Result add(@RequestBody Publicity publicity){
        int num = publicityService.add(publicity);
        if (num!=0){
            return Result.success("添加成功!");
        }
        else {
            return Result.error("添加失败!");
        }
    }

    @DeleteMapping("/{id}")
    private Result remove(@PathVariable("id") Long id){
        int num = publicityService.remove(id);
        if (num!=0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PutMapping("/edit")
    public Result edit( @RequestBody Publicity publicity){
        System.out.println(publicity);
     publicityService.edit(publicity);
          return   Result.success("修改成功");

    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Long id) {
        Publicity publicity = publicityService.findById(id);
        return Result.success(publicity);
    }

    @GetMapping("/findAll")
    public Result findAll(Publicity publicity){
        List<Publicity> list = publicityService.findAll(publicity);
        return Result.success(list);
    }

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5") Integer pageSize,
            Publicity publicity
    ){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Publicity> list = publicityService.findByPage(pageNum,pageSize,publicity);
        return Result.success(list);
    }

}
